 package nc.ui.bd.mes.dmoys.view;

 
 import java.util.Vector;
 import nc.ui.ml.NCLangRes;
 import nc.ui.pub.ButtonObject;
 import nc.ui.pub.beans.UIButton;
 import nc.ui.pub.hotkey.HotkeyUtil;
 import nc.vo.ml.AbstractNCLangRes;
 import nc.vo.ml.NCLangRes4VoTransl;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 public class ButtonUtils
 {
   public static final String BTN_OK = "BTN_OK";
   public static final String BTN_CANCEL = "BTN_CANCEL";
   public static final String BTN_REFRESH = "BTN_REFRESH";
   public static final String BTN_VIEW = "BTN_VIEW";
   public static final String BTN_ADD = "BTN_ADD";
   public static final String BTN_DELETE = "BTN_DELETE";
   public static final String BTN_MODIFY = "BTN_MODIFY";
   public static final String BTN_ADDLINE = "BTN_ADDLINE";
   public static final String BTN_DELLINE = "BTN_DELLINE";
   public static final String BTN_COPYLINE = "BTN_COPYLINE";
   public static final String BTN_PASTELINE = "BTN_PASTELINE";
   public static final String BTN_INSERTLINE = "BTN_INSERTLINE";
   public static final String BTN_PRINT = "BTN_PRINT";
   public static final String BTN_SAVE = "BTN_SAVE";
   public static final String BTN_CLEAR = "BTN_CLEAR";
   public static final String BTN_CLOSE = "BTN_CLOSE";
   private static final String[] buttonNames = { "BTN_PRINT", "BTN_REFRESH", "BTN_VIEW", "BTN_ADD", "BTN_MODIFY", "BTN_DELETE", "BTN_ADDLINE", "BTN_DELLINE", "BTN_INSERTLINE", "BTN_COPYLINE", "BTN_PASTELINE", "BTN_OK", "BTN_CANCEL", "BTN_SAVE", "BTN_CLEAR", "BTN_CLOSE" };
   
 
 
 
 
 
   private static final int[] buttonOrders = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
   
 
 
   private static final String[] buttonObjectNames = { NCLangRes4VoTransl.getNCLangRes().getStrByID("common", "UC001-0000007"), NCLangRes4VoTransl.getNCLangRes().getStrByID("common", "UC001-0000006"), NCLangRes4VoTransl.getNCLangRes().getStrByID("common", "UC001-0000009"), NCLangRes4VoTransl.getNCLangRes().getStrByID("5001003_0", "05001003-0001"), NCLangRes4VoTransl.getNCLangRes().getStrByID("common", "UC001-0000002"), NCLangRes4VoTransl.getNCLangRes().getStrByID("common", "UC001-0000045"), NCLangRes4VoTransl.getNCLangRes().getStrByID("common", "UC001-0000039"), NCLangRes4VoTransl.getNCLangRes().getStrByID("common", "UC001-0000012"), NCLangRes4VoTransl.getNCLangRes().getStrByID("common", "UC001-0000013"), NCLangRes4VoTransl.getNCLangRes().getStrByID("common", "UC001-0000016"), NCLangRes4VoTransl.getNCLangRes().getStrByID("common", "UC001-0000014"), NCLangRes4VoTransl.getNCLangRes().getStrByID("common", "UC001-0000015"), NCLangRes4VoTransl.getNCLangRes().getStrByID("common", "UC001-0000044"), NCLangRes4VoTransl.getNCLangRes().getStrByID("common", "UC001-0000008"), NCLangRes4VoTransl.getNCLangRes().getStrByID("common", "UC001-0000001"), NCLangRes4VoTransl.getNCLangRes().getStrByID("5001003_0", "05001003-0002"), NCLangRes4VoTransl.getNCLangRes().getStrByID("5001003_0", "05001003-0003") };
   
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
   private static final int[] buttonObjectOrders = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 };
   
 
 
 
 
 
   public static UIButton createButton(String buttonName)
   {
     UIButton button = new UIButton();
     button.setName(buttonName);
     
     String common = "common";
     if (buttonName.equals("BTN_OK")) {
       button.setText(NCLangRes.getInstance().getStrByID(common, "UC001-0000044"));
       HotkeyUtil.setHotkey(button, 'Y');
     }
     else if (buttonName.equals("BTN_CANCEL")) {
       button.setText(NCLangRes.getInstance().getStrByID(common, "UC001-0000008"));
       HotkeyUtil.setHotkey(button, 'C');
     }
     else if (buttonName.equals("BTN_REFRESH")) {
       button.setText(NCLangRes.getInstance().getStrByID(common, "UC001-0000009"));
       HotkeyUtil.setHotkey(button, 'R');
     }
     else if (buttonName.equals("BTN_ADD")) {
       button.setText(NCLangRes.getInstance().getStrByID(common, "UC001-0000002"));
       HotkeyUtil.setHotkey(button, 'N');
     }
     else if (buttonName.equals("BTN_DELETE")) {
       button.setText(NCLangRes.getInstance().getStrByID(common, "UC001-0000039"));
       HotkeyUtil.setHotkey(button, 'D');
     }
     else if (buttonName.equals("BTN_CANCEL")) {
       button.setText(NCLangRes.getInstance().getStrByID(common, "UC001-0000008"));
       HotkeyUtil.setHotkey(button, 'U');
     }
     else if (buttonName.equals("BTN_MODIFY")) {
       button.setText(NCLangRes.getInstance().getStrByID(common, "UC001-0000045"));
       HotkeyUtil.setHotkey(button, 'E');
     }
     else if (buttonName.equals("BTN_ADDLINE")) {
       button.setText(NCLangRes.getInstance().getStrByID(common, "UC001-0000012"));
       HotkeyUtil.setHotkey(button, 'M');
     }
     else if (buttonName.equals("BTN_DELLINE")) {
       button.setText(NCLangRes.getInstance().getStrByID(common, "UC001-0000013"));
       HotkeyUtil.setHotkey(button, 'D');
     }
     else if (buttonName.equals("BTN_COPYLINE")) {
       button.setText(NCLangRes.getInstance().getStrByID(common, "UC001-0000014"));
       HotkeyUtil.setHotkey(button, 'F');
     }
     else if (buttonName.equals("BTN_PASTELINE")) {
       button.setText(NCLangRes.getInstance().getStrByID(common, "UC001-0000015"));
       HotkeyUtil.setHotkey(button, 'G');
     }
     else if (buttonName.equals("BTN_INSERTLINE")) {
       button.setText(NCLangRes.getInstance().getStrByID(common, "UC001-0000016"));
       HotkeyUtil.setHotkey(button, 'I');
     }
     else if (buttonName.equals("BTN_PRINT")) {
       button.setText(NCLangRes.getInstance().getStrByID(common, "UC001-0000007"));
       HotkeyUtil.setHotkey(button, 'P');
     }
     else if (buttonName.equals("BTN_SAVE")) {
       button.setText(NCLangRes4VoTransl.getNCLangRes().getStrByID("common", "UC001-0000001"));
       HotkeyUtil.setHotkey(button, 'S');
     }
     else if (buttonName.equals("BTN_CLEAR")) {
       button.setText(NCLangRes4VoTransl.getNCLangRes().getStrByID("5001003_0", "05001003-0002"));
       
 
 
       HotkeyUtil.setHotkey(button, 'M');
     }
     else if (buttonName.equals("BTN_CLOSE")) {
       button.setText(NCLangRes4VoTransl.getNCLangRes().getStrByID("5001003_0", "05001003-0003"));
       
 
 
       HotkeyUtil.setHotkey(button, 'Q');
     }
     else if (buttonName.equals("BTN_VIEW")) {
       button.setText(NCLangRes4VoTransl.getNCLangRes().getStrByID("5001003_0", "05001003-0001"));
       
 
 
       HotkeyUtil.setHotkey(button, 'R');
     }
     else {
       button.setText(buttonName);
       int idx = buttonName.indexOf("(");
       if (idx > 0) {
         button.setText(buttonName.substring(0, idx));
         HotkeyUtil.setHotkey(button, buttonName.charAt(idx + 1));
       }
     }
     return button;
   }
   
 
 
 
   public static UIButton[] sortButtons(UIButton[] buttons)
   {
     UIButton[] result = (UIButton[])sortBubble(buttons);
     
 
     int idxClose = getIndexByButtonName(result, "BTN_CLOSE");
     moveToIndex(result, idxClose, result.length - 1);
     
 
     int idxCancel = getIndexByButtonName(result, "BTN_CANCEL");
     moveToIndex(result, idxCancel, result.length - 2);
     
     int idxOk = getIndexByButtonName(result, "BTN_OK");
     moveToIndex(result, idxOk, result.length - 3);
     
     return result;
   }
   
 
 
 
   public static ButtonObject[] sortButtons(ButtonObject[] buttons)
   {
     ButtonObject[] result = (ButtonObject[])sortBubble(buttons);
     
     int idxClose = getIndexByButtonName(result, "BTN_CLOSE");
     moveToIndex(result, idxClose, result.length - 1);
     
 
     int idxCancel = getIndexByButtonName(result, NCLangRes4VoTransl.getNCLangRes().getStrByID("common", "UC001-0000008"));
     
 
 
 
 
     moveToIndex(result, idxCancel, result.length - 2);
     
     int idxOk = getIndexByButtonName(result, NCLangRes4VoTransl.getNCLangRes().getStrByID("common", "UC001-0000044"));
     
 
 
 
 
     moveToIndex(result, idxOk, result.length - 3);
     
     return result;
   }
   
 
 
 
   private static int getButtonOrder(Object button)
   {
     String buttonName = null;
     String[] names = null;
     int[] orders = null;
     if ((button instanceof UIButton)) {
       buttonName = ((UIButton)button).getName();
       names = buttonNames;
       orders = buttonOrders;
     }
     else {
       buttonName = ((ButtonObject)button).getCode();
       names = buttonObjectNames;
       orders = buttonObjectOrders;
     }
     
     for (int i = 0; i < names.length; i++) {
       if (buttonName.equals(names[i])) {
         return orders[i];
       }
     }
     
     return -1;
   }
   
   private static int getIndexByButtonName(UIButton[] buttons, String buttonName)
   {
     for (int i = 0; i < buttons.length; i++) {
       if (buttons[i].getName().equals(buttonName)) {
         return i;
       }
     }
     
     return -1;
   }
   
   private static int getIndexByButtonName(ButtonObject[] buttons, String buttonName)
   {
     for (int i = 0; i < buttons.length; i++) {
       if (buttons[i].getCode().equals(buttonName)) {
         return i;
       }
     }
     
     return -1;
   }
   
   private static Object[] sortBubble(Object[] buttons)
   {
     int moveCount = 0;
     Vector vecOhterBtns = new Vector();
     for (int i = 0; i < buttons.length - moveCount; i++)
     {
       int minIndex = i;
       int minOrder = getButtonOrder(buttons[i]);
       if (minOrder < 0) {
         vecOhterBtns.add(buttons[i]);
         if (i < buttons.length - 1)
         {
           moveOneForward(buttons, i + 1, buttons.length);
         }
         moveCount++;
         i--;
       }
       else
       {
         for (int j = i + 1; j < buttons.length - moveCount; j++) {
           int order = getButtonOrder(buttons[j]);
           if (order >= 0)
           {
 
             if (order < minOrder) {
               minIndex = j;
               minOrder = order;
             }
           }
         }
         if (minIndex > i) {
           Object button = buttons[i];
           buttons[i] = buttons[minIndex];
           buttons[minIndex] = button;
         }
       }
     }
     
     if (moveCount > 0)
     {
       int idx = 0;
       for (int i = buttons.length - moveCount; i < buttons.length; i++) {
         buttons[i] = vecOhterBtns.elementAt(idx++);
       }
     }
     
     return buttons;
   }
   
 
 
 
   private static Object[] moveOneForward(Object[] ary, int idxBegin, int idxEnd)
   {
     System.arraycopy(ary, idxBegin, ary, idxBegin - 1, idxEnd - idxBegin);
     return ary;
   }
   
 
 
 
   private static Object[] moveToIndex(Object[] ary, int indexSrc, int indexDest)
   {
     if ((indexSrc < 0) || (indexSrc >= indexDest)) {
       return ary;
     }
     
     Object src = ary[indexSrc];
     moveOneForward(ary, indexSrc + 1, indexDest + 1);
     ary[indexDest] = src;
     
     return ary;
   }
 }

